package com.qf.fronted.servlet;

import com.qf.admin.domain.Address;
import com.qf.admin.domain.User;
import com.qf.admin.service.AddressService;
import com.qf.admin.service.impl.AddressServiceImpl;
import com.qf.base.BaseServlet;
import com.qf.commons.Data;
import com.qf.utils.CookieUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Objects;

@WebServlet(value = "/addressServlet", name = "FrontedAddressServlet")
public class AddressServlet extends BaseServlet {

    private AddressService addressService = new AddressServiceImpl();
    
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String methodName = req.getParameter("method");

        try {
            Method method = AddressServlet.class.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改用户的收货地址
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void changeTakeDeliveryAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 需要更改的收货地址
        String addressId = req.getParameter("addressId");

        //通过cookie获取userId
        int userId = Integer.parseInt(Objects.requireNonNull(CookieUtils.getCookie(req, "userId")));

        Data data = null;
        try {
            addressService.changeTakeDeliveryAddress(userId, Integer.parseInt(addressId));
            data = new Data(1, "success");
        } catch (Exception ex) {
            ex.printStackTrace();
            data = new Data(-1, "更改收货地址错误");
        }
        sendJson(resp, data);
    }

    //修改为默认地址
    private void changeDefaultAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 需要更改的收货地址
        String addressId = req.getParameter("addressId");

        //通过cookie获取userId
        int userId = Integer.parseInt(Objects.requireNonNull(CookieUtils.getCookie(req, "userId")));

        Data data = null;
        try {
            addressService.changeDefaultAddress(userId, Integer.parseInt(addressId));
            data = new Data(1, "success");
        } catch (Exception ex) {
            ex.printStackTrace();
            data = new Data(-1, "更改收货地址错误");
        }
        sendJson(resp, data);
    }

    /**
     * 获取当前用户的收货地址
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void getAddressOfUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过cookie获取userId
        int userId = Integer.parseInt(Objects.requireNonNull(CookieUtils.getCookie(req, "userId")));

        // 获取当前用户的收货地址
        List<Address> addresses = addressService.getAddressByUserId(userId);

        sendJson(resp, addresses);
    }

    private void addOrUpdateAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过cookie获取userId
        int userId = Integer.parseInt(Objects.requireNonNull(CookieUtils.getCookie(req, "userId")));
        //获取请求参数
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String detail = req.getParameter("detail");
        String state = req.getParameter("state");
        String aid = req.getParameter("aid");
        if (null == aid) {
            //添加
            addressService.addAddress(userId, name, phone, detail, Integer.parseInt(state));
            sendJson(resp, "地址添加成功");
        } else {
            //修改
            addressService.updateAddress(userId, name, phone, detail, Integer.parseInt(aid));
            sendJson(resp, "地址修改成功");
        }
    }

    private void deleteAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过cookie获取userId
        int userId = Integer.parseInt(Objects.requireNonNull(CookieUtils.getCookie(req, "userId")));
        //获取请求参数
        String aid = req.getParameter("aid");
        addressService.deleteAddress(userId, Integer.parseInt(aid));
        sendJson(resp, "删除成功");
    }

    private void getAddressByAid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String aid = req.getParameter("aid");
        Address address = addressService.getAddressByAid(Integer.parseInt(aid));
        sendJson(resp, address);
    }
}
